﻿@page "/counter"
@using BlazorMobile.Common
@using BlazorMobile.InteropApp.Common.Interfaces
@using BlazorMobile.Common.Services
@using BlazorMobile.InteropBlazorApp.Helpers 
@inject IXamarinBridge XamarinBridge
@implements IDisposable

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<button class="btn btn-primary" @onclick="AlertMe">Alert me</button>
<button class="btn btn-primary" @onclick="FaultyTaskCall">Fault Task call</button>
<br />
<button class="btn btn-primary" @onclick="PostMessageTest">Remote PostMessage Test With String</button>
<button class="btn btn-primary" @onclick="PostMessageTestBool">Remote PostMessage Test With Bool</button>
<button class="btn btn-primary" @onclick="PostMessageTestLocal">Local PostMessage Test With String</button>
<br />
<button class="btn btn-primary" @onclick="CallJSInvokableMethodTest">Remote CallJSInvokableMethod Test</button>
<br /><br />
<button class="btn btn-primary" @onclick="SwitchToAnotherAppPackage">Switch current app package to 'AnotherApp' package</button>

@code {
    int currentCount = 0;

    void IncrementCount()
    {
        Console.WriteLine("IncrementCount !");
        currentCount++;
    }

    async void AlertMe()
    {
        Console.WriteLine("AlertMe clicked !");
        await XamarinBridge.DisplayAlert("Platform identity", $"Current platform is {BlazorDevice.RuntimePlatform}", "Great!");
    }

    async void FaultyTaskCall()
    {
        try
        {
            await XamarinBridge.CallFaultyTask();
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("Call faulty task", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }

    public async void OnMessageReceived(string payload)
    {
        await XamarinBridge.DisplayAlert("Message received!", payload, "OK");
    }

    async void PostMessageTest()
    {
        try
        {
            BlazorMobileService.MessageSubscribe<string>("PostMessageTest", OnMessageReceived);

            await XamarinBridge.TriggerPostMessageTest();
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("PostMessageTest", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }

    public async void OnMessageReceivedBoolean(bool payload)
    {
        await XamarinBridge.DisplayAlert("Message received!", payload.ToString(), "OK");
    }

    async void PostMessageTestBool()
    {
        try
        {
            BlazorMobileService.MessageSubscribe<bool>("PostMessageTest", OnMessageReceivedBoolean);

            await XamarinBridge.TriggerPostMessageTestBool();
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("PostMessageTest", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }

    async void PostMessageTestLocal()
    {
        try
        {
            BlazorMobileService.MessageSubscribe<string>("PostMessageTest", OnMessageReceived);
            BlazorMobileService.PostMessage("PostMessageTest", "Local PostMessage test");
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("PostMessageTest", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }

    [JSInvokable]
    public static void InvokableMethodTest(string myMessage)
    {
        ServicesHelper.GetXamarinBridge().DisplayAlert("JSInvoke received!", myMessage, "OK");
    }

    async void CallJSInvokableMethodTest()
    {
        ServicesHelper.SetXamarinBridge(XamarinBridge);

        try
        {
            await XamarinBridge.TriggerJSInvokableTest();
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("CallJSInvokableMethodTest", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }

    public void Dispose()
    {
        BlazorMobileService.MessageUnsubscribe<string>("PostMessageTest", OnMessageReceived);
        BlazorMobileService.MessageUnsubscribe<bool>("PostMessageTest", OnMessageReceivedBoolean);
    }

    async void SwitchToAnotherAppPackage()
    {
        ServicesHelper.SetXamarinBridge(XamarinBridge);

        try
        {
            await XamarinBridge.SwitchToAnotherAppPackage();
        }
        catch (Exception ex)
        {
            await XamarinBridge.DisplayAlert("SwitchToAnotherAppPackage", $"{ex.Message}", "OK");
            Console.WriteLine($"{ex.Message}");
        }
    }
}
